//
//  HMComposeTextView+Extension.swift
//  WeiBo
//
//  Created by Feng on 2017/6/16.
//  Copyright © 2017年 feng. All rights reserved.
//

import UIKit

extension HMComposeTextView
{
    //  获取富文本对应的微博内容
    // --->计算属性
    var emoticonText: String
    {
        //  在指定范围遍历textView的富文本获取对应的表情描述和普通的字符串，然后进行拼接发送给后台
        var result = ""
        
        self.attributedText.enumerateAttributes(in: NSMakeRange(0, self.attributedText.length), options: []) { (info, range, _) in
            //  info: 富文本信息 -->是一个字典
            //  range: 富文本对应的范围
            //  nsrange转字符串
            print(NSStringFromRange(range))
            
            if let attachment = info["NSAttachment"] as? HMEmoticonTextAttachment
            {
                //  图片表情， 表情富文本
                let chs = attachment.emoticon!.chs!
                result += chs
            }
            else
            {
                //普通字符串富文本
                let subAttributeStr = self.attributedText.attributedSubstring(from: range)
                //将富文本转为字符串
                let text = subAttributeStr.string
                result += text
            }
        }
        
        return result
    }
    
    func insertEmoticon(emoticon: HMEmoticon)
    {
        if emoticon.type == "0"
        {
            //图片表情
            let lastEmoticonAttributeText = NSMutableAttributedString(attributedString: self.attributedText)
//            // 1.创建UIImage对象
//            let image = UIImage(named: emoticon.path!, in: EmoticonButton, compatibleWith: nil)
//            // 2.创建文本附件(NSTextAttachment)
////            let textAttachment = NSTextAttachment()
//            let textAttachment = HMEmoticonTextAttachment()
//            textAttachment.emoticon = emoticon
//            //获取文字高度
//            let lineHeight = self.font!.lineHeight
//            //更改富文本的bounds,使文字能够和富文本对齐
//            textAttachment.bounds = CGRect(x: 0, y: -4, width: lineHeight, height: lineHeight)
//            // 3.设置文本附件对应的图片对象
//            textAttachment.image = image
//            // 4.根据文本附件创建富文本
//            let emoticonAttributeText = NSAttributedString(attachment: textAttachment)
            //            lastEmoticonAttributeText.append(emoticonAttributeText)
            // 5.设置富文本
            let emoticonAttributeText = NSAttributedString.attributedStringWithEmoticon(emoticon: emoticon, font: self.font!)
            //设置光标选中范围
            var range = self.selectedRange
            //替换选中范围-> 富文本
            lastEmoticonAttributeText.replaceCharacters(in: range, with: emoticonAttributeText)
            //统一设置富文本的文字大小
            lastEmoticonAttributeText.addAttributes([NSFontAttributeName: UIFont.systemFont(ofSize: 15)], range: NSMakeRange(0, lastEmoticonAttributeText.length))
            
            //设置光标的位置
            range.location += 1
            range.length = 0
            self.selectedRange = range
            
            self.attributedText = lastEmoticonAttributeText
            
            //设置富文本不会 发送通知或者 执行代理方法
            //所以手动发送通知 和 执行代理方法
            NotificationCenter.default.post(name: NSNotification.Name.UITextViewTextDidChange, object: nil)
            self.delegate?.textViewDidChange!(self)
        }
        else
        {
            //emoji表情
            let emoji = (emoticon.code! as NSString).emoji()
            //  设置textView内容, 插入到光标后面
            self.insertText(emoji!)
        }
    }
}
